package com.zmn.plat.admin.controller.product.tariff;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.plat.model.entity.product.tariff.ProductTariffQuery;
import com.zmn.plat.model.vo.product.tariff.ProductTariffVO;

/**
 * 产品故障价格表，每个产品价格表一条数据，配置价格表说明、状态等
 *
 * @author linfeng
 * @since 2019-11-21 18:57:24
 */
@RestController
@RequestMapping("/product/tariff/")
public class ProductTariffModifyController extends ProductTariffBaseController {
    private final static int EXPORT_EXCEL_SIZE = 10000;

    /**
     * 修改状态
     *
     * @param ids
     * @param status
     * @author linfeng
     * @since 2019-11-21 18:57:24
     */
    @RequestMapping(value = "updateStatus")
    @RequiresPermissions("/product/tariff/updateStatus.action")
    @SuppressWarnings("Duplicates")
    public ResponseDTO updateStatus(HttpServletRequest request, @RequestParam(value = "ids", defaultValue = "") String ids,
        @RequestParam(value = "status", defaultValue = "0") Integer status) {
        if (StringUtil.isBlank(ids)) {
            return ResponseDTO.fail("请选择要更改状态的数据！");
        }
        if (status == 0) {
            return ResponseDTO.fail("请设置状态！");
        }

        List<Integer> arrayIds = StringUtil.convertStrsToList(ids);
        List<ProductTariffVO> list = productFaultTariffService.findByKeys(arrayIds);
        if (status.equals(GlobalConsts.NO)) {
            ProductTariffVO defaultTariff = list.stream().filter(e -> e.getGeneral().equals(GlobalConsts.YES)).findFirst().orElse(null);
            if (defaultTariff != null) {
                return ResponseDTO.fail("默认价格表不能被停用！");
            }
        }

        productFaultTariffService.updateStatusBatchByKey(arrayIds, status, getMcStaffRealName());
        return ResponseDTO.success(ids, GlobalConsts.SUCCESS_MSG);
    }

    /**
     * 描述: 添加产品保外故障价格表
     *
     * @param productFaultTariffVO
     *            产品故障价格表
     * @return com.zmn.common.dto2.ResponseDTO
     * @author linfeng
     * @since 2019/11/27
     **/
    @RequestMapping(value = "saveProductPriceTable")
    public ResponseDTO saveProductPriceTable(@RequestBody ProductTariffVO productFaultTariffVO) {

        try {
            return baseSaveProductPriceTable(productFaultTariffVO);
        } catch (Exception e) {
            logger.error("添加产品保外故障价格表操作失败,e:{},msg:{}", e, e.getMessage());
            return ResponseDTO.fail("操作失败！");
        }
    }

    @RequestMapping(value = "judgeExportExcel")
    public ResponseDTO judgeExportExcel(@RequestBody ProductTariffQuery query) {
        // 默认导出10000行
        Integer totalNum = productFaultTariffService.countByQuery(query);
        if (totalNum > EXPORT_EXCEL_SIZE) {
            return ResponseDTO.fail("导出数据超过一万行，请根据查询条件分批次导出！");
        }

        return ResponseDTO.success(totalNum, GlobalConsts.SUCCESS_MSG);
    }
}
